package com.qf.web;

import com.qf.pojo.Dept;
import com.qf.service.DeptService;
import com.qf.service.impl.DeptServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;


@WebServlet("/addDept")
public class AddDeptServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        DeptService deptService=new DeptServiceImpl();
        PrintWriter writer = resp.getWriter();
        int id = Integer.parseInt(req.getParameter("id"));
        String dname = req.getParameter("dname");
        String loc = req.getParameter("loc");

        //查
        List<Dept> list = deptService.findDeptById(id);
        if(list.size()!=0){
            writer.println("您要添加的部门信息已存在");
        }else {
            //添加
            int num = deptService.addDept(new Dept(id, dname, loc));
            if(num==1){
               resp.sendRedirect("/findDeptAll");
            }else {
                writer.println("部门信息添加失败");
            }
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
